import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * AssignDisciplineWindow.java
 * 
 * Provides a window to provide a list of disciplines to for assigning
 * 
 * @author Steven Glazer
 * @author Kyle Marcotte
 * @author Sam Milton
 */
public class AssignDisciplineWindow extends JDialog implements ActionListener, ListSelectionListener {
	
	private static final long serialVersionUID = -1987272009263189086L;
	
	// Class Variables
	private final int HGAP = 12;
	private final int VGAP = 12;
	
	private JList listData;
	private JScrollPane listDataScroll;
	private JPanel pane;
	private JPanel inputPane;
	private JPanel mainPane;
	private JPanel buttonPane;
	private JButton selectBut;
	private JButton cancelBut;
	private JTextField reasonInput;
	private JTextField dateInput;
	private JLabel descriptionLabel;
	private JLabel punishmentLabel;
	private boolean selected = false;
	private Discipline[] data;
	
	/**
	 * Constructor
	 */
	public AssignDisciplineWindow(JFrame parent, boolean modal, Discipline[] data) {
		super(parent, modal);
		this.data = data;
		this.setTitle("Assign Discipline");
		pane = (JPanel)this.getContentPane();
		pane.setLayout(new BorderLayout(HGAP, VGAP));
		pane.setBorder(BorderFactory.createEmptyBorder(VGAP, HGAP, VGAP, HGAP));
		
		buttonPane = new JPanel();
		buttonPane.setLayout(new GridLayout(1, 2, HGAP, VGAP));
		inputPane = new JPanel();
		inputPane.setLayout(new GridLayout(4, 1, HGAP, VGAP));
		mainPane = new JPanel();
		mainPane.setLayout(new GridLayout(2, 1, HGAP, VGAP));
		
		String[] discNames = new String[data.length];
		for (int i = 0; i < data.length; i++) {
			discNames[i] = "" + data[i].code;
		}
		
		listData = new JList(discNames);
		listDataScroll = new JScrollPane(listData);
		selectBut = new JButton("Select");
		selectBut.addActionListener(this);
		cancelBut = new JButton("Cancel");
		cancelBut.addActionListener(this);
		
		DateFormat dFormat = new SimpleDateFormat("MM/dd/yyyy");
		Date date = new Date();
		dateInput = new JTextField(dFormat.format(date));
		reasonInput = new JTextField("Reason", 40);
		
		descriptionLabel = new JLabel("Description:");
		punishmentLabel = new JLabel("Punishment:");
		
		listData.addListSelectionListener(this);
		
		listData.setSelectedIndex(0);
		
		inputPane.add(reasonInput);
		inputPane.add(dateInput);
		inputPane.add(descriptionLabel);
		inputPane.add(punishmentLabel);
		
		mainPane.add(inputPane);
		mainPane.add(listDataScroll);
		
		buttonPane.add(selectBut);
		buttonPane.add(cancelBut);
		
		pane.add(mainPane, BorderLayout.CENTER);
		pane.add(buttonPane, BorderLayout.SOUTH);
		
		this.pack();
		this.setLocationRelativeTo(parent);
		this.setVisible(true);
	}
	
	/**
	 * actionPerformed method for ActionListener
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectBut)
			selected = true;
		this.setVisible(false);
	}
	
	public void valueChanged(ListSelectionEvent e) {
		int index = listData.getSelectedIndex();
		if (index != -1) {
			descriptionLabel.setText("Description: " + data[index].description);
			punishmentLabel.setText("Punishment: " + data[index].punishment);
		}
	}
	
	/**
	 * Returns the index of the club selected to assign
	 * 
	 * @return	Index of the club to assign
	 */
	public int getResult() {
		if(selected)
			return listData.getSelectedIndex();
		else
			return -1;
	}
	
	/**
	 * Returns the information for the discipline
	 * 
	 * @return	An array containing the reason and the expiration date
	 */
	public String[] getInfo() {
		if(selected) {
			String[] info = new String[2];
			info[0] = reasonInput.getText();
			info[1] = dateInput.getText();
			return info;
		}
		else
			return null;
	}
}
